package com.taxsystem.gaozhuanglin.booking;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import com.main.taxsystem.R;
import com.taxsystem.gaozhuanglin.bookingtool.EggCalendar;
import com.taxsystem.gaozhuanglin.bookingtool.SchDate;
import com.taxsystem.tool.HttpUtil;

import org.json.JSONArray;
import org.json.JSONObject;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Spinner;

public class CalendarActivity extends Activity {
	
	private EggCalendar c;
	private Spinner sp;
	private EditText et;
	private Button btn_next;
	//Button btn;
	//private int [] schDateIds;
	//private int areaId;
	//private int ticketItemId;
	private String [] timeSlices = {"8:00~8:30(2)", "8:30~9:00(1)", "9:00~9:30(2)", "9:30~10:00(4)",
			                        "10:00~10:30(0)", "10:30~11:00(1)", "11:00~11:30(1)", "11:30~12:00(5)",
			                        "12:00~12:30(3)", "12:30~13:00(2)", "13:00~13:30(3)", "13:30~14:00(2)",
			                        "14:00~14:30(4)", "14:30~15:00(2)", "15:00~15:30(0)", "15:30~16:00(1)",
			                        "16:00~16:30(2)", "16:30~17:00(0)", "17:00~17:30(1)", "17:30~18:00(0)"};
	public final static String RECI_COAST_CALENDAR = "CALENDAR";
	private final static String url = "";
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.calendar_date);
		
		c = (EggCalendar)findViewById(R.id.calendar_time);
		sp = (Spinner)findViewById(R.id.ts);
		et = (EditText)findViewById(R.id.gp);
		btn_next = (Button)findViewById(R.id.nb);
		
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.time_item, timeSlices);
		sp.setAdapter(adapter);
		
		btn_next.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(CalendarActivity.this, BookingFinal.class);
				startActivity(intent);
			}
			
		});
		
		c.setOnTouchListener(new OnTouchListener(
				) {
			
			@Override
			public boolean onTouch(View v, MotionEvent event) {
				// TODO Auto-generated method stub
				switch (event.getAction()) {
				case MotionEvent.ACTION_DOWN:
					float x = event.getX();
					float y = event.getY();
					if(c.isSwitchMonth(x, y)) {
						
					}
					else  {
						
					}
					break;
				case MotionEvent.ACTION_UP:
					break;
				}
				return false;
			}
		});
        //LinearLayout calendar = (LinearLayout)findViewById(R.id.calendar_container);
        //btn = (Button)findViewById(R.id.yesBtn);
        
        //areaId = getIntent().getIntExtra("areaId", 1);
        //ticketItemId = getIntent().getIntExtra("ticketItemId", 1);
		
		//获取日历组件
		//float density = getResources().getDisplayMetrics().density;
		//c = new EggCalendar(this);
		//calendar.addView(c, new LinearLayout.LayoutParams((int)(260*density), (int)(280*density)));
		
		//IntentFilter filter = new IntentFilter(RECI_COAST_CALENDAR);
		//BroadcastReceiver myRecive = new CalendarRecive();
		//registerReceiver(myRecive, filter);
		
		Thread schDateThread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST_CALENDAR);
				try {
					//获取服务器返回的信息
					String reslut = HttpUtil.getRequest(url);
					intent.putExtra("calendar_date", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
		//schDateThread.start();
		
		/*btn.setOnClickListener(new OnClickListener()
		{

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent3 = new Intent(CalendarActivity.this, TimeSliceActivity.class);
				intent3.putExtra("areaId", areaId);
				intent3.putExtra("ticketItemId", ticketItemId);
				intent3.putExtra("schDateId", 1);
				startActivity(intent3);
				
			}});*/
	}
	
	/*private class CalendarRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try
			{
				JSONObject json = new JSONObject(intent.getStringExtra("calendar_Date"));
				JSONArray jsonArray = json.getJSONArray("Data");
				SchDate [] schDateArray = new SchDate[jsonArray.length()];
				schDateIds = new int[jsonArray.length()];
				for(int i = 0; i < jsonArray.length(); i++)
				{
					JSONObject j1 = (JSONObject)jsonArray.get(i);
					schDateArray[i].setSchDateId(j1.getInt("schDateId"));
					schDateArray[i].setDateValue(j1.getString("dateValue"));
					schDateArray[i].setCreditCount(j1.getInt("creditCount"));
					schDateArray[i].setCalYear(j1.getInt("calYear"));
					schDateArray[i].setCalMonth(j1.getInt("calMonth"));
					schDateArray[i].setCalDate(j1.getInt("calDate"));
					schDateArray[i].setCalDay(j1.getInt("calDay"));
					schDateArray[i].setBookSchCount(j1.getInt("bookSchCount"));
					schDateArray[i].setBookedCount(j1.getInt("bookedCount"));
					schDateIds[i] = j1.getInt("schDateId");
				}
			}
			
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}*/
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_tab, menu);
		return true;
	}
}
